package util;

import java.util.List;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.c3p0.C3p0Plugin;

public class JFinalUtils {
	
	public static final String DEV = "Dev";
	public static final String UAT = "Uat";
	public static final String PRD = "Prd";
	
	public static Prop propDev = PropKit.use("jfinal/main-dev-conf.properties");
	public static Prop propUat = PropKit.use("jfinal/main-uat-conf.properties");
	public static Prop propPrd = PropKit.use("jfinal/main-prd-conf.properties");

	private JFinalUtils(){}
	
	static {
		//Dev
		C3p0Plugin dsDev = new C3p0Plugin(propDev.get("db_url"), 
				propDev.get("db_username"), 
				propDev.get("db_password"));
		ActiveRecordPlugin arpDev = new ActiveRecordPlugin(DEV, dsDev);
		dsDev.start();
		arpDev.start();
		//Uat
		C3p0Plugin dsUat = new C3p0Plugin(propUat.get("db_url"), 
				propUat.get("db_username"), 
				propUat.get("db_password"));
		ActiveRecordPlugin arpUat = new ActiveRecordPlugin(UAT, dsUat);
		dsUat.start();
		arpUat.start();
		//Prd
		C3p0Plugin dsPrd = new C3p0Plugin(propPrd.get("db_url"), 
				propPrd.get("db_username"), 
				propPrd.get("db_password"));
		ActiveRecordPlugin arpPrd = new ActiveRecordPlugin(PRD, dsPrd);
		dsPrd.start();
		arpPrd.start();
	}
	
	public static List<Record> fetchApiInfo(String apiId, String appSubId, String configName){
		List<Record> list = null;
		StringBuilder sbb = new StringBuilder();
		sbb.append(" SELECT  b.org_id, b.app_secret, b.token, b.sys_id,c.version, a.api_id,a.app_pub_id,a.app_sub_id");
		sbb.append(" FROM api_sub_interface a LEFT JOIN api_app b ON a.app_sub_id = b.app_id");
		sbb.append(" LEFT JOIN api_interface c ON a.api_id = c.api_id");
		sbb.append(" WHERE a.api_id = '").append(apiId).append("'");
		sbb.append(" AND a.app_sub_id = '").append(appSubId).append("'");
		sbb.append(" AND a.del_flag = 0 AND b.del_flag = 0");
		String sql = sbb.toString();
		list = Db.use(configName).find(sql);
		return list;
	}
}
